﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Maticsoft.Web.stu
{
    public partial class stuReg : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {

            if (Page.IsValid)
            {
                string stuName = this.txtstuName.Text;
                string stusex = "男";
                if (rdbwoman.Checked) stusex = "女";
                string stuAddress = this.txtstuAddress.Text;
                string stuNum = this.txtstuNum.Text;               
                string stuPost = this.txtstuPost.Text;
                string stuPhone = this.txtstuPhone.Text;
                string stuType = ddltype.SelectedItem.Text;
                string stuPic = "";
                string IndFile = "";
                bool iserror = false;
                if (fuIndFile.HasFile)
                {
                    String name = fuIndFile.FileName;
                    String exp = name.Substring(name.LastIndexOf('.')).ToLower();
                    String filename = DateTime.Now.ToString("yyyyMMddhhmmssfff");
                    if (exp == ".jpg" || exp == ".gif" || exp == ".png")
                    {
                        if (fuIndFile.PostedFile.ContentLength <= 50 * 1024 * 1024)
                        {
                            try
                            {
                                fuIndFile.PostedFile.SaveAs(Server.MapPath("~/upload/" + filename + exp));
                                IndFile = "/upload/" + filename + exp;
                            }
                            catch (Exception ex)
                            {
                                iserror = true;

                                ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('上传文件失败！');", true);
                            }
                        }
                        else
                        {
                            iserror = true;

                            ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('文件大于50m');", true);
                        }
                    }
                    else
                    {

                        ClientScript.RegisterClientScriptBlock(this.GetType(), "a", "alert('文件格式不正确');", true);

                        iserror = true;
                    }

                }
                if (iserror == true)
                {
                    Maticsoft.Common.MessageBox.Show(this, "保存失败！");
                }
                else
                {
                    Maticsoft.Model.studentInfo model = new Maticsoft.Model.studentInfo();
                    model.stuName = stuName;
                    model.stusex = stusex;
                    model.stuAddress = stuAddress;
                    model.stuNum = stuNum;
                    model.state = 0;
                    model.stuPost = stuPost;
                    model.stuPhone = stuPhone;
                    model.stuType = stuType;
                    model.stuPic = IndFile;
                    Maticsoft.BLL.studentInfo bll = new Maticsoft.BLL.studentInfo();
                    int id = bll.Add(model);

                    model.stuId = id;
                    Session["login"] = model;
                    Maticsoft.Common.MessageBox.ShowAndRedirect(this, "注册成功！", "frame.aspx");
                }
            }

        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }
    }
}